package com.ai.study.dashscope.config;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.api.DashScopeResponseFormat;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
// import io.milvus.client.MilvusServiceClient;
// import io.milvus.param.ConnectParam;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author chenwei
 * @Date 2025/5/27 10:30
 */
@RequiredArgsConstructor
@Configuration
public class ChatClientAndModelConfiguration {

    @Bean(name = "dashScopeChatClient")
    public ChatClient dashScopeChatClient(DashScopeChatModel chatModel) {
        // 构造时，可以设置 ChatClient 的参数
        // {@link org.springframework.ai.chat.client.ChatClient};
        return ChatClient.builder(chatModel)
                // 设置默认System Message
                // .defaultSystem("你将作为一名 Spring-AI-Alibaba 的专家，对于用户使用的需求作出解答")
                // 实现 Logger 的 Advisor
                .defaultAdvisors(
                        new SimpleLoggerAdvisor()
                )
                // 设置 ChatClient 中 ChatModel 的 Options 参数
                .defaultOptions(
                        DashScopeChatOptions.builder()
                                .withTopP(0.7)
                                .build()
                )
                .build();
    }

    @Bean(name = "dashScopeChatClientDefaultSystem")
    public ChatClient dashScopeChatClientDefaultSystem(DashScopeChatModel chatModel) {
        // 构造时，可以设置 ChatClient 的参数
        // {@link org.springframework.ai.chat.client.ChatClient};
        return ChatClient.builder(chatModel)
                // 设置默认System Message
                .defaultSystem("You are a friendly chat bot that answers question in the voice of a {voice}")
                // 实现 Logger 的 Advisor
                .defaultAdvisors(
                        new SimpleLoggerAdvisor()
                )
                // 设置 ChatClient 中 ChatModel 的 Options 参数
                .defaultOptions(
                        DashScopeChatOptions.builder()
                                .withTopP(0.7)
                                .build()
                )
                .build();
    }

    @Bean(name = "dashScopeChatClientCommon")
    public ChatClient dashScopeChatClientCommon(DashScopeChatModel chatModel) {
        // 构造时，可以设置 ChatClient 的参数
        // {@link org.springframework.ai.chat.client.ChatClient};
        return ChatClient.builder(chatModel)
                .build();
    }

    @Bean("milvusChatClient")
    public ChatClient milvusChatClient(DashScopeChatModel chatModel) {
        // 构造时，可以设置 ChatClient 的参数
        // {@link org.springframework.ai.chat.client.ChatClient};
        return ChatClient.builder(chatModel)
                // 实现 Logger 的 Advisor
                .defaultAdvisors(
                        new SimpleLoggerAdvisor()
                )
                // 设置 ChatClient 中 ChatModel 的 Options 参数
                .defaultOptions(
                        DashScopeChatOptions.builder()
                                .withTopP(0.7)
                                .build()
                )
                .defaultSystem("你将作为一名 Spring-AI-Alibaba 的专家，对于用户的使用需求作出解答")
                .build();
    }

    @Bean(name = "dashscopeChatModel")
    public ChatModel dashscopeChatModel(DashScopeChatModel dashscopeChatModel) {
        // dashscopeChatModel.setDashScopeChatOptions(
        //         DashScopeChatOptions.builder()
        //                 .withModel(DashScopeApi.ChatModel.QWEN_PLUS.getModel())
        //                 .withTopP(0.7)
        //                 .withMaxToken(1024)
        //                 .build()
        // );
        return dashscopeChatModel;
    }

    @Bean(name = "dashscopeChatModelForGraph")
    public ChatModel dashscopeChatModelForGraph(DashScopeChatModel dashscopeChatModel) {
        dashscopeChatModel.setDashScopeChatOptions(
                DashScopeChatOptions.builder()
                        .withModel(DashScopeApi.ChatModel.QWEN_PLUS.getModel())
                        .build()
        );
        return dashscopeChatModel;
    }

    @Bean(name = "dashscopeChatClientForGraph")
    public ChatClient dashscopeChatClientForGraph(@Qualifier("dashscopeChatModelForGraph") ChatModel chatModel) {
        return ChatClient.builder(chatModel)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor()
                ).build();
    }
}
